package com.chen.pspms.service.impl;

import com.chen.pspms.entity.Resourcedemand;
import com.chen.pspms.mapper.ResourcedemandMapper;
import com.chen.pspms.service.ResourcedemandService;
import com.chen.pspms.utils.ApplicationException;
import com.chen.pspms.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ResourcedemandServiceImpl implements ResourcedemandService {
    @Autowired
    ResourcedemandMapper resourcedemandMapper;

    @Override
    public Integer save(Resourcedemand resourcedemand) throws ApplicationException {
        int row=0;
        try{
            row=resourcedemandMapper.save(resourcedemand);
        }catch(Exception e){
            throw new ApplicationException("新增资源需求失败",e);
        }
        return row;
    }

    @Override
    public Integer delete(Integer id) throws ApplicationException {
        int row=0;
        try{
            row=resourcedemandMapper.delete(id);
        }catch(Exception e){
            throw new ApplicationException("删除资源需求失败",e);
        }
        return row;
    }

    @Override
    @RedisCache(key = "#id")
    public Resourcedemand findyById(Integer id) throws ApplicationException {
        Resourcedemand resourcedemand;
        try{
            resourcedemand=resourcedemandMapper.findyById(id);
        }catch(Exception e){
            throw new ApplicationException("查询资源需求失败",e);
        }
        return resourcedemand;
    }

    @Override
    @RedisCache(key = "#type")
    public Resourcedemand findyByType(String type) throws ApplicationException {
        Resourcedemand resourcedemand;
        try{
            resourcedemand=resourcedemandMapper.findyByType(type);
        }catch(Exception e){
            throw new ApplicationException("根据类型查询资源需求失败",e);
        }
        return resourcedemand;
    }

    @Override
    public Integer update(Resourcedemand resourcedemand) throws ApplicationException {
        int row=0;
        try{
            row=resourcedemandMapper.update(resourcedemand);
        }catch(Exception e){
            throw new ApplicationException("更新资源需求失败",e);
        }
        return row;
    }

    @Override
    public List<Resourcedemand> findyAll() throws ApplicationException {
        List<Resourcedemand> resourcedemandlist;
        try{
            resourcedemandlist=resourcedemandMapper.findyAll();
        }catch(Exception e){
            throw new ApplicationException("查询资源需求列表失败",e);
        }
        return resourcedemandlist;
    }
}
